function delay(fn,delayTime){
    return Promise.resolve({
        then:function(resolve,reject){
            setTimeout(function() {
                resolve(fn());
            }, delayTime);
        }
    })
}

function timeoutPromise(time){
    return new Promise((resolve,reject)=>{
        setTimeout(function() {
            reject("time out:"+time);
        }, time);
    })
}

var p=delay(function(){
    return "delay finish";
},100);
var p2=new Promise(function(resolve,reject){
    setTimeout(function() {
        resolve("delay 200ms");
    }, 200);
})

//Promise.race：闩，这是一种竞态关系。第一个成功则整个都成功，一个失败则整个都失败
Promise.race([p,p2])
.then(function fulfilled(value){
    console.log(value);
})

//超时竞赛，一旦超过指定时间则报错
Promise.race([p,timeoutPromise(300)])
.then(
    function fulfilled(value){
        console.log("finish");
    },
    function rejected(value){
        console.log("error:"+value);
    }
)